package com.jiuyun.servlet;

import com.google.gson.Gson;
import com.jiuyun.dao.ClassesDao;
import com.jiuyun.dao.impl.ClassesDaoImpl;
import com.jiuyun.service.ClassService;
import com.jiuyun.service.impl.ClassServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Set;

@WebServlet("/classes/*")
public class ClassesServlet extends BaseServlet {

    ClassesDao dao=new ClassesDaoImpl();
    ClassService service=new ClassServiceImpl();
    Gson gson=new Gson();

    protected void find(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String cid= request.getParameter("sid");
        Integer id=Integer.parseInt(cid);
        String rq=request.getParameter("rq");
        System.out.println(cid);
        String yue=rq.substring(5,7);
        Integer yue1=Integer.parseInt(yue);
        String nian=rq.substring(0,4);
        Integer nian1=Integer.parseInt(nian);
        System.out.println(yue);
        System.out.println(nian);
        List list=null;
        List list2=null;
        List list3=null;
        try {
            list=dao.getStudents(id);
            list2=dao.getClasses();
            list3=dao.getHomework(id,nian1,yue1);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        request.setAttribute("list",list);
        request.setAttribute("list2",list2);
        request.setAttribute("list3",list3);
        request.getRequestDispatcher("/homework.jsp").forward(request,response);
    }

    protected void select(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String cid= request.getParameter("sid");
        Integer id=Integer.parseInt(cid);
        String rq=request.getParameter("rq");
        System.out.println(cid);
        String yue=rq.substring(5,7);
        Integer yue1=Integer.parseInt(yue);
        String nian=rq.substring(0,4);
        Integer nian1=Integer.parseInt(nian);
        HashMap<String,int[]> info = service.homework(id,nian1,yue1);
        request.setAttribute("info",info);
        request.getRequestDispatcher("/home.jsp").forward(request,response);
    }
    protected void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        String id=request.getParameter("stid");
        Integer stid=Integer.parseInt(id);
        String date=request.getParameter("date");
        String bz=request.getParameter("bz");
        int a=dao.add(stid,date,bz);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out=response.getWriter();
        out.print("{\"code\":"+a+"}");
        out.flush();
        out.close();
    }
    protected void ban(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        List list=dao.getClasses();
        String p=gson.toJson(list);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out=response.getWriter();
        out.print(p);
        out.flush();
        out.close();
    }
    protected void stu(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, SQLException {
        String id=request.getParameter("stid");
        List list=dao.getStudents(Integer.parseInt(id));
        String p=gson.toJson(list);
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        PrintWriter out=response.getWriter();
        out.print(p);
        out.flush();
        out.close();
    }
}
